﻿<script type="text/javascript">
    boolSearch = true;
    function initDetailGrid() {
        $('#btnApply').linkbutton('enable');

        $.InitControlDateTime("#ApplyDateTime");
        $("#AppleyRemark").textbox("clear");
        $('#ApplyName').textbox('setValue', $('#userName').val());
        var urlstr1 = '@Url.Content("~/IM/AllDictionary/GetStogageByUser?userId=")' + $('#userID').val();
        $.BindCombox("#ApplyStorage", urlstr1);

        $("#gridApplyDetailList").datagrid({
            scrollbarSize: 18,
            rownumbers: true,
            singleSelect: true,
            width: 'auto',
            height: 245,
            fitColumns: true,
            striped: true,
            singleSelect: true,
            loadMsg: '数据加载中请稍后……',
            pagination: false,//启用分页，默认每页10行
            columns: [
                [
                   { title: '名称', field: 'MaterialName', width: '10%', sortable: false },
                   { title: '批次号', field: 'RealBatchNo', width: '10%', sortable: false, formatter: $.formatRealBatchNo },
                   { title: '有效期', field: 'ValidityDate', width: '10%', sortable: false, formatter: $.formatValidityDate },
                   { title: '仓库', field: 'StorageName', width: '10%', sortable: false },
                   { title: '申领时间', field: 'DeliveryDetailDateTime', width: '15%', sortable: false },
                   {
                       field: 'DeliveryDetailCounts', title: '申领数量', width: '10%',
                       formatter: function (value, rec, rowIndex) {
                           return "<input id=\"ApplyCounts" + rowIndex + "\" name = \"ApplyCounts\"  style=\"width:80px\" value='1'/>";
                       }
                   },
                   {
                       field: 'DeliveryDetailRemark', title: '备注', width: '30%',
                       formatter: function (value, rec, rowIndex) {
                           return "  <input id=\"ApplyDetailRemark" + rowIndex + "\" name = \"ApplyDetailRemarks\"/>";
                       }
                   },
                   { title: 'StorageCode', field: 'StorageCode', sortable: false, hidden: true },
                   { title: 'ID', field: 'ID', sortable: false, hidden: true },
                   { title: 'MaterialID', field: 'MaterialID', sortable: false, hidden: true }
                ]
            ],
            toolbar: [
                    {
                        id: 'btnDetailDelete',
                        text: '删除申领信息',
                        iconCls: 'icon-custom-Delete',
                        handler: function () {
                            var selected = $('#gridApplyDetailList').datagrid('getSelected');
                            if (selected) {
                                deleteDetailJsondate(selected.ID);
                            } else {
                                $.messager.alert('提示', '请选择数据行', 'info', function () {
                                });
                            }
                        }
                    },
                    {
                        id: 'btnBZ',
                        text: '<span style="color:red;font-size:large">(贵重药品：除一次性颈托和气管插管外其他按盒领)</span>',
                    }
            ]
        });
        $("#gridApplyDetailList").datagrid("loadData", Materials);
        setControl();
    } 
    function submitDeliveryDetailForm() {
        var isValid = $("#ApplyDetailList").form("validate");
        if (!isValid) {
            $.showMsg("请填写必填项！", "提示");
        } else {
            $('#btnApply').linkbutton('disable');
            var jsData = new Array();
            var i = 0;
            var IsSameStorage = 0;//20151130相同的仓库
            var IsSameMaterial = 0;//20151130判断是否相同物资，相同批次等
            var ApplyStorage = $("#ApplyStorage").combotree("getValue");//20151103物资去向
            var rows = $('#gridApplyDetailList').datagrid('getRows');
            var limit = false;//新加限制功能判断：贵重药品申领数量超过10，不给提交  2016-07-07  yh

            $("input[name='ApplyCounts']").each(function () {
                var obj = new Object();
                obj.ID = rows[i].ID;
                //20151130增加如果物资去向与要出库所在库一样的就不允许出库
                if (ApplyStorage == rows[i].StorageCode) {
                    IsSameStorage = 1;
                }
                obj.MaterialID = rows[i].MaterialID;
                obj.BatchNo = rows[i].BatchNo;
                obj.RealBatchNo = rows[i].RealBatchNo;
                obj.TargetStorageCode = rows[i].StorageCode;
                obj.Remark = "";
                obj.DeliveryCounts = $(this).val();
                //20151130--增加判断要出库的物资，如果是相同物资，并且批次号相同，只是所在仓库不同，不允许出库；
                $.each(jsData, function (i, info) {
                    if (info.MaterialID == obj.MaterialID && info.BatchNo == obj.BatchNo && info.RealBatchNo == obj.RealBatchNo) {
                        IsSameMaterial = 1;
                    }
                });
                if (IsSameMaterial == 0) {
                    jsData.push(obj);
                }               
                //----start  新加限制功能判断：贵重药品申领数量超过10，不给提交  2016-07-07  yh
                if ((obj.MaterialID == 246 || obj.MaterialID == 248 || obj.MaterialID == 250 || obj.MaterialID == 255
                    || obj.MaterialID == 260 || obj.MaterialID == 272 || obj.MaterialID == 294 || obj.MaterialID == 374
                    || obj.MaterialID == 328 || obj.MaterialID == 495) && obj.DeliveryCounts > 10) {
                    $.messager.alert('提示', '有贵重药品申领数量超过上限10，请重新填写', 'info', function () { });
                    $('#btnApply').linkbutton('enable');
                    limit = true;
                    return false;
                }
                //----end                       
                i++;
            });

            if (IsSameMaterial == 1) {
                $.showMsg("同物资并且同批次号不允许同时出库！请分批次申领！", "提示");//20151103
                $('#btnApply').linkbutton('enable');
                return;
            }
            if (IsSameStorage == 1) {
                $.showMsg("物资去向与要出库的仓库相同不允许申领出库！", "提示");//20151103
                $('#btnApply').linkbutton('enable');
            }
            else {               
                if (!limit) {
                    var j = 0;
                    $("input[name='ApplyDetailRemarks']").each(function () {
                        jsData[j].Remark = $(this).val();
                        j++;
                    });
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Content("~/IM/I_Apply/SaveApply")',
                        data:
                            {
                                ApplyUserID: $('#userID').val(),
                                ApplyDateTime: $("#ApplyDateTime").datebox('getValue'),
                                ApplyStorage: $("#ApplyStorage").combotree("getValue"),
                                Remark: $("#AppleyRemark").val(),
                                sApplyDetailInfo: JSON.stringify(jsData)
                            },
                        dataType: "json",
                        success: function (jsData) {
                            $.handleJsonResult(jsData, function () {
                                Materials.length = 0;
                                $("#gridSurplusList").datagrid("reload");
                                $('#ApplyDetailList').dialog("close");
                            }, function () {
                                $('#btnApply').linkbutton('enable');
                            });
                        }
                    });
                }
            }
        }
    }
    function deleteDetailJsondate(ecode) {
        var obj;
        for (var i = 0; i < Materials.length; i++) {
            if (ecode == Materials[i].ID) {
                obj = Materials[i];
                break;
            }
        }
        Materials.remove(obj);
        $("#gridApplyDetailList").datagrid("loadData", Materials);
        changeDeliveryViewNum();
        setDeliveryViewNumCheck();
        setControl();
    }
    function setControl() {
        for (var i = 0; i < Materials.length; i++) {
            $.initDataGridTextBoxNum("#ApplyCounts" + i, 999999999, true);
            $('#ApplyDetailRemark' + i).textbox({
                multiline: true,
                width: 200,
                height: 30
            });
        }
    }
</script>
<div id="ApplyDetailList" class="easyui-dialog" title="申领单详细清单" data-options="modal:true,collapsible:false,minimizable:false,maximizable:false,closed:true,resizable:false,iconCls:'icon-custom-cGoodsFull'" style="width:1000px;height:400px;padding:0px;">
    <div data-options="region:'north'" style="overflow: hidden; padding: 10px;width:900px;">
        <table>
            <tr>
                <td width="100px" style="vertical-align:text-top;text-align:right">申领时间：</td>
                <td width="200px" style="vertical-align:text-top">
                    <input id="ApplyDateTime" class="easyui-datetimebox" style="width:150px" data-options="required:true" />
                </td>
                <td width="100px" style="vertical-align: text-top; text-align: right">申领人：</td>
                <td width="200px"><input id="ApplyName" class="easyui-textbox" data-options="readonly:true" /></td>
                <td width="100px" style="vertical-align: text-top; text-align: right">申领去向：</td>
                <td width="200px"><input id="ApplyStorage" class="easyui-combotree" data-options="required:true" /></td>
            </tr>
            <tr>
                <td width="100px" style="vertical-align:text-top;text-align:right">备注：</td>
                <td colspan="4">
                    <input id="AppleyRemark" class="easyui-textbox" data-options="multiline:true" style="width:500px;height:50px">
                </td>
                <td style="text-align:right; vertical-align:bottom">
                    <a href="javascript:void(0)" id="btnApply" class="easyui-linkbutton" data-options="iconCls:'icon-custom-cDelivery'" onclick="submitDeliveryDetailForm()">&nbsp;&nbsp;确认申领&nbsp;&nbsp;</a>
                    &nbsp;&nbsp;
                </td>
            </tr>
        </table>
    </div>
    <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
        <table id="gridApplyDetailList"></table>
    </div>
</div>